package com.hxkj.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.domain.po.*;
import com.hxkj.service.AuthUserService;
import com.hxkj.service.SiteService;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;

/**
 * @ClassName UserAuthController  用户控制器
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/5/19 14:46
 * @Version 1.0
 */

@RestController
@RequestMapping(value = "/user")
public class AuthUserController {

    @Autowired
    private AuthUserService authUserService;

    @Autowired
    private SiteService siteService;


    /**
    * @Description getlist 获取用户列表
    **/
    @RequiresPermissions("user:view")
    @RequestMapping(value = "/getlist",  method = RequestMethod.POST)
    public Callable<ResultPageStruct<AllocationAuthUser>> getUserBySlave(@ModelAttribute AuthUser userAuth , @ModelAttribute PageTo pageParam ) {
        return () -> authUserService.getAuthUserBySlave(userAuth == null ? new AuthUser() : userAuth,pageParam);
    }

    /**
    * @Description get  获取用户信息
    **/
    @RequiresPermissions("user:view")
    @RequestMapping(value = "/get",  method = RequestMethod.POST)
    public Callable<JSONObject> getAuthUser(@ModelAttribute AuthUser userAuth) {
        return () -> authUserService.getAuthUser(userAuth);
    }

    /**
    * @Description save 新增用户
    **/
    @RequiresPermissions("user:add")
    @RequestMapping(value = "/save",  method = RequestMethod.POST)
    public Callable<ResultMessageStruct> saveUser(@ModelAttribute AuthUser authUser)  {
        return () -> authUserService.saveAuthUser(authUser);
    }

    /**
    * @Description modify 修改用户
    **/
    @RequiresPermissions("user:modify")
    @RequestMapping(value="/modify", method = RequestMethod.POST)
    public Callable<ResultMessageStruct> updateAuthUser(@ModelAttribute AuthUser authUser)  {
        return () -> authUserService.updateAuthUser(authUser);
    }

    /**
    * @Description remove 删除用户信息
    **/
    @RequiresPermissions("user:remove")
    @RequestMapping(value = "/remove",  method = RequestMethod.POST)
    public  Callable<ResultMessageStruct> deleteAuthUser(@ModelAttribute AuthUser authUser ) {
        return () -> authUserService.deleteAuthUser(authUser);
    }


    /**
     * 根据角色查找用户
     * @param authRole
     * @return
     * @throws Exception
     */
    @RequiresPermissions("user:view")
    @RequestMapping(value = "/role/getlist", method = RequestMethod.POST)
    public  Callable<AllocationUser> getRoleAssignUser(@ModelAttribute AuthRole authRole){
        return () ->authUserService.getRoleAssignUser(authRole);
    }

    /**
     * 添加用户到角色
     * @param authRole
     * @return
     * @throws Exception
     */
    @RequiresPermissions("role:add")
    @RequestMapping(value = "/addusertorole", method = RequestMethod.POST)
    public  Callable<ResultMessageStruct> addAuthRoleToUser(@ModelAttribute AuthRole authRole) {

        return () -> authUserService.addUsertToRole(authRole);
    }

    /**
     * @Description saveUserSite 添加用户名下站点删除修改
     **/
    @RequiresPermissions("user:modify")
    @RequestMapping(value = "/setsite", method = RequestMethod.POST)
    public Callable<ResultMessageStruct> saveUserSite(@ModelAttribute AuthUser authUser){
        return () -> siteService.saveUserSite(authUser);
    }

    /**
     * @Description getUserSite 根据用户ID查询其管理的站点
     **/
    @RequiresPermissions("user:view")
    @RequestMapping(value = "/getsite", method = RequestMethod.POST)
    public Callable<AllocationSite> getUserSite(@ModelAttribute AuthUser authUser) {
        return () -> siteService.getUserSiteList(authUser);
    }

    /**
     * @Description modify 用户重置密码
     **/
    @RequiresPermissions("user:modify")
    @RequestMapping(value="/modifypassword", method = RequestMethod.POST)
    public void updateAuthUserPassword(@ModelAttribute AuthUser authUser) throws Exception {
        authUserService.updateAuthUserPassword(authUser);
    }
}
